/* ==================================================================   
 * Created [2009-4-27 下午11:32:55] by Jon.King 
 * ==================================================================  
 * TSS 
 * ================================================================== 
 * mailTo:jinpujun@hotmail.com
 * Copyright (c) Jon.King, 2009-2012 
 * ================================================================== 
 */

package com.jinhe.tss.core.cachepool;

import com.jinhe.tss.core.cachepool.container.ContainerFactory;
import com.jinhe.tss.core.cachepool.container.IPoolContainer;
import com.jinhe.tss.core.exception.BusinessException;
import com.jinhe.tss.core.util.BeanUtil;


/** 
 * <p> SimplePool.java </p> 
 * 简单的缓存对象池，适合简单的缓存需求。
 * 
 * @author Jon.King 2007-1-3
 */
public class SimplePool extends AbstractPool {

    private IPoolContainer poolContainer;  // 对象池容器

    public SimplePool(){
    }
    
    public final void init() {
        final Class<?> collectionType = BeanUtil.createClassByName(getCacheStrategy().getPoolCollectionClass());
        if (!IPoolContainer.class.isAssignableFrom(collectionType)) {
            throw new BusinessException("指定的池集合类类型非法: " + collectionType.getName() + " (必须实现cachepool.container.IPoolContainer接口)");
        }
        
        final ContainerFactory factory = ContainerFactory.getInstance();
        poolContainer = factory.create(collectionType.getName(), getCacheStrategy().getCode());
   
        log.info("缓存池（" + strategy.getName() + "）初始化成功！");
    }

    public void release(final boolean forced) {
        if (released) return;
        
        poolContainer.clear(); 
        released = true;
    }

    public IPoolContainer getFree() { return poolContainer; }

    public IPoolContainer getUsing() { return poolContainer; }

    public synchronized int getSize() { return poolContainer.size(); }
}

